package com.chinanet.netelementmodel;

import com.chinanet.entry.netelement.Attr;
import com.chinanet.entry.netelement.AttrType;
import com.chinanet.entry.netelement.WorkInterFace;
import com.chinanet.utils.webservice.AttrsIn;
import com.chinanet.utils.webservice.WebService;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author zsf
 * @Desc Created by myseital on 2017/12/13.
 */
public class PGAccess_Imp implements WorkInterFace {

    @Override
    public String getName() {
        return "公安绿色通道查询接口";
    }

    @Override
    public String getDesciption() {
        return "";
    }

    @Override
    public int getVersion() {
        return 0;
    }

    @Override
    public Attr[] getInAttrs() {
        Attr[] in=new Attr[2];
        in[0]=new Attr("ip","IP", AttrType.TYPE_String,true);//入参 ip
        in[1]=new Attr("port","端口",AttrType.TYPE_String,true);//入参  端口号
        return in;
    }

    @Override
    public Attr[] getOutAttrs() {
        Attr[] out = new Attr[4];
        out[0]=new Attr("ReturnCode", "0表示成功，1表示交互失败2业务账号/宽带账号不存在3 频道名称不匹配4 超时5 其他", AttrType.TYPE_Int, true);
        out[1]=new Attr("Report", "内容", AttrType.TYPE_String, true);
        out[2]=new Attr("log", "服务器返回的soapxml", AttrType.TYPE_String, true);
        out[3]=new Attr("Errormsg", "服务器返回的错误信息,当ReturnCode为0的时候就不必去读取", AttrType.TYPE_String, false);
        return out;
    }

    @Override
    public Map<String, Object> init(Map<String, Object> data) {
        AttrsIn attrsIn=new AttrsIn();
        WebService webService=new WebService();
        Map<String,Object> map=new HashMap<String, Object>();
        //Get into arguments
        attrsIn.setClientSysName("CDMA");
        attrsIn.setFunctionName("Ipsearch");
        attrsIn.setRequestMessage("<ip>"+(String)data.get("ip")
                +"</ip><port>"+(String)data.get("port")
                +"</port>");
        //Get soapxml
        String returnXml=webService.WebServiceInit(attrsIn);
        String flag=webService.getPattern("flag", returnXml);
        if(flag.equals("true")){
            map.put("Report", webService.getPattern("Report", returnXml));
            map.put("log", webService.dealkouhao(returnXml));
            map.put("flag",1);
            map.put("Errormsg", "");
        }else{
            map.put("flag",0);
            map.put("Report", "");
            map.put("log", webService.dealkouhao(returnXml));
            map.put("Errormsg", "IP反查接口处出现错误，处理请求失败");
        }
        return map;
    }
}
